package yxy.game.pm2.bean.module.rank;

import org.joda.time.DateTime;
import org.redisson.api.RBucket;

import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.module.BaseGameRedisBean;

public class RankInfo extends BaseGameRedisBean {
	// bean===========================================================================

	private String rankKey;
	private DateTime updateTime;

	public String getRankKey() {
		return rankKey;
	}

	public void setRankKey(String rankKey) {
		this.rankKey = rankKey;
	}

	public DateTime getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(DateTime updateTime) {
		this.updateTime = updateTime;
	}

	// redis===========================================================================

	/** 排行榜信息key */
	static protected String key(String rankKey) {
		return String.format("%s:INFO", rankKey);
	}

	static public RankInfo get(String rankKey) {
		RankInfo rankInfo = null;
		try {
			RBucket<RankInfo> bucket = touchBucket(key(rankKey));
			rankInfo = bucket.get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (rankInfo == null) {
			rankInfo = create(rankKey);
		}
		return rankInfo;
	}

	private static RankInfo create(String rankKey) {
		RankInfo rankInfo = new RankInfo();
		rankInfo.setUpdateTime(DateTime.now());
		rankInfo.setRankKey(rankKey);
		rankInfo.set();
		return rankInfo;
	}

	protected void set() {
		try {
			setv(key(rankKey), this);
		} catch (RedisException e) {
		}
	}

	// lock===========================================================================
	@Override
	public String lockKey() {
		return null;
	}
}
